import machine,time,network,_thread
from machine import Pin,ADC,I2C,UART
from i2c_lcd import I2cLcd

uart=UART(1, baudrate=9600, tx=Pin(17), rx=Pin(16))
#串口初始化
uart.write('XBOX CONTROLLER READY')


led=Pin(2,Pin.OUT)
led.value(1)
data_i=0
DAC_L1=DAC(26)
DAC_L2=DAC(27)
DAC_R1=DAC(28)
DAC_R2=DAC(29)
DAC_L1.write(data_i)
DAC_L2.write(data_i)
DAC_R1.write(data_i)
DAC_R2.write(data_i)
#左右遥杆输出
DAC_L=DAC(25)
DAC_R=DAC(33)
DAC_L.write(0)
DAC_R.write(0)
#左右扳机位输出
Pin_UP=Pin(32,Pin.OUT)
Pin_DOWN=Pin(35,Pin.OUT)
Pin_LEFT=Pin(34,Pin.OUT)
Pin_RIGHT=Pin(36,Pin.OUT)
#方向键输出
Pin_X=Pin(39,Pin.OUT)
Pin_Y=Pin(38,Pin.OUT)
Pin_A=Pin(37,Pin.OUT)
Pin_B=Pin(23,Pin.OUT)
#按键输出
Pin_LB=Pin(19,Pin.OUT)
Pin_RB=Pin(18,Pin.OUT)
Pin_A.value(data_i)
Pin_B.value(data_i)
Pin_X.value(data_i)
Pin_Y.value(data_i)
Pin_UP.value(data_i)
Pin_DOWN.value(data_i)
Pin_LEFT.value(data_i)
Pin_RIGHT.value(data_i)
Pin_LB.value(data_i)
Pin_RB.value(data_i)
#肩键输出
i2c = I2C(0, scl=Pin(22), sda=Pin(21), freq=100000)
lcd = I2cLcd(i2c, 0x27, 4, 20)  # 4行20列
lcd.putstr("XBOX CONTROLLER READY")
#LCD初始化
for i in range(3,1,-1):
    lcd.putstr('XBOX CONTROLLER READY',str(i))
    time.sleep(1)
    lcd.clear()


def net():
    lcd.clear()
    lcd.putstr("Connecting to WiFi")
    wlan = network.WLAN(network.STA_IF)  # 创建工作站模式对象
    wlan.active(True)  # 激活接口
    wifi_name='your wifi name'
    wifi_password='your wifi password'
    lcd.putstr("Connecting to WiFi",wifi_name)
    wlan.connect(wifi_name, wifi_password)  # 连接WiFi

    while not wlan.isconnected():
        lcd.clear()
        for i in range(3,1,-1):
            lcd.putstr('wifi not ready',str(i))
            time.sleep(1)
            lcd.clear()
            break
        else:
            lcd.clear()
            lcd.putstr("WiFi connected")
            lcd.putstr("IP address: ", wlan.ifconfig()[0])
            time.sleep(1)
            lcd.clear()
            break
test_data_off=[3.3,3.3,3.3,3.3,3.3,3.3,1,1,1,1,1,1,1,1,1,1]
test_data_on=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]#归零位
#测试数据

def controller(DAC_L1,DAC_L2,DAC_R1,DAC_R2,DAC_L,DAC_R,Pin_UP,Pin_DOWN,Pin_LEFT,Pin_RIGHT,Pin_X,Pin_Y,Pin_A,Pin_B,Pin_LB,Pin_RB):
    DAC_L1.write(DAC_L)
    DAC_L2.write(DAC_L2)
    DAC_R1.write(DAC_R1)
    DAC_R2.write(DAC_R2)
    DAC_L.write(DAC_L)
    DAC_R.write(DAC_R)
    Pin_UP.value(Pin_UP)
    Pin_DOWN.value(Pin_DOWN)
    Pin_LEFT.value(Pin_LEFT)
    Pin_RIGHT.value(Pin_RIGHT)
    Pin_X.value(Pin_X)
    Pin_Y.value(Pin_Y)
    Pin_A.value(Pin_A)
    Pin_B.value(Pin_B)
#控制主函数
uart.trady()
lcd.clear()
lcd.putstr("LINK TO PC UART")

while uart.ready()=='test':
    lcd.clear()
    lcd.putstr("xbox controller test")
    time.sleep(1)
    lcd.clear()
    lcd.putstr("----------3---------")
    time.sleep(1)
    lcd.clear()
    lcd.putstr("----------2---------")
    time.sleep(1)
    lcd.clear()
    lcd.putstr("----------1---------")
    time.sleep(0.5)
    lcd.clear()
    lcd.putstr("xbox controller test off")
    controller(test_data_off)
    time.sleep(3)
    lcd.clear()
    lcd.putstr("xbox controller test on")
    controller(test_data_on)
    time.sleep(2)
    lcd.clear()
    lcd.putstr("xbox controller DAC test")
    for i in range(0,3.3,0.01):
        test_data_dac=[i,i,i,i,i,i,0,0,0,0,0,0,0,0,0,0]
        controller(test_data_dac)
        time.sleep(1)
    lcd.clear()
    lcd.putstr("xbox controller test over")
    controller(test_data_on)
    break
    
if uart.ready()=='PC READY':
    lcd.clear()
    lcd.putstr("LINK TO PC UART")
    time.sleep(1)
    lcd.clear()
    lcd.putstr("----------3---------")
    time.sleep(1)
    lcd.clear()
    lcd.putstr("----------2---------")
    time.sleep(1)
    lcd.clear()
    lcd.putstr("----------1---------")
    time.sleep(0.5)
    while True:
        lcd.clear()
        led.value(0)
        data=uart.readline()
        led.value(1)
        controller(str(data))
        print(str(data))
        lcd.putstr(str(data))